<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getActivitys">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="活动名称" width="120" sortable>
			</el-table-column>
      <el-table-column prop="dictionary.title" label="活动优惠方式" width="120" sortable>
      </el-table-column>
      <el-table-column prop="startDate" label="开始时间" width="120" sortable>
      </el-table-column>
      <el-table-column prop="endDate" label="结束时间" width="120" sortable>
      </el-table-column>
      <el-table-column prop="description" label="活动名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="120" sortable>
      </el-table-column>
      <el-table-column prop="create.username" label="创建人" width="120" sortable>
      </el-table-column>
      <el-table-column prop="editTime" label="结束时间" width="120" sortable>
      </el-table-column>
      <el-table-column prop="edit.username" label="结束人" width="120" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="prev, pager, next"
          :current-page="query.currentPage"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          style="float:right;">
			</el-pagination>
		</el-col>


		<!--新增界面-->
		<el-dialog title="新增/修改" :visible.sync="activityFormVisible" :close-on-click-modal="false">
			<el-form :model="activityForm" label-width="80px" :rules="activityFormRules" ref="activityForm">
        <el-form-item label="名称" prop="name">
					<el-input v-model="activityForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="活动优惠方式">
          <el-input v-model="dictionary.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" >
          <el-date-picker  v-model="activityForm.startDate" type="date"  placeholder="选择时间" size="medium"> </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" >
          <el-date-picker  v-model="activityForm.endDate" type="date"  placeholder="选择时间" size="medium"> </el-date-picker>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="activityFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="activityFormLoading">提交</el-button>
			</div>
		</el-dialog>
  </section>
</template>

<script>
	export default {
		data() {
			return {
				query: {
					currentPage: 1,
          pageSize: 10,
          keyword:""
				},
        pageInfo:{
				  total:0,
          rows:[]
        },
				listLoading: false,
				sels: [],//列表选中列

        activityFormVisible: false,//新增界面是否显示
        activityFormLoading: false,
        activityFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
        Dictionary:[],
        deptTree:[],


				//新增界面数据
				activityForm: {
				  id:null,
					name: "",
					title: "",
          Dictionary:{
				    title:""
          },
          startDate:"",
          endDate:""
				}
			}
		},
		methods: {
			search(){
			  this.query.currentPage=1;
			  this.getActivitys()
      },
			handleCurrentChange(val) {
        this.query.currentPage= val;
				this.getActivitys();
			},
			//获取用户列表
			getActivitys() {
			  this.listLoading=true;
				this.$http.post("/activity",this.query)
            .then(result=>{
              result=result.data;
              console.log(result);
              if (result.success){
                this.pageInfo=result.result;
                this.listLoading=false;
              }else {
                this.$message({
                  message:'查询失败'+result.message,
                  type: "ERROR"
                    })
              }
            }).catch(result=>{
          this.$message({message:"查询失败", type: "ERROR"})
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.delete("/activity/"+row.id)
              .then(result=>{
                result=result.data;
                  if (result.success){
                    this.$message({message: '删除成功', type: 'success'});
                    this.query.currentPage=1;
                    this.getActivitys();
                    this.listLoading = false;
                  }else {
                    this.$message({message:"删除失败"+result.message, type: "ERROR"})
                  }
              }).catch(()=>{
            this.$message({message:"删除失败", type: "ERROR"})
          })
        })
			},
      getEmployeeList(){
			  this.$http.get("/employee")
            .then(result=>{
			    result=result.data;
			    if (result.success){
			      this.employees=result.result;
          }else {
			      this.$message({message:'获取员工列表失败'+result.message,type:'ERROR'})
          }
        }).catch(result=>{
          this.$message({message:'获取员工列表失败',type:'ERROR'});
        })
      },
      getDepartTree(){
        this.$http.get("/activity/tree")
            .then(result=>{
              result=result.data;
              if (result.success){
                this.deptTree=result.result;
              }else {
                this.$message({message:'获取部门列表失败'+result.message,type:'ERROR'})
              }
            }).catch(result=>{
          this.$message({message:'获取部门列表失败',type:'ERROR'});
        })
      },
			//显示编辑界面
			handleEdit: function (index, row) {
				this.activityFormVisible = true;
				this.activityForm = Object.assign({}, row);
				if (this.activityForm.manage==null){
          this.activityForm.manage={id:null}
        }
        if (row.path != null){
          let arrays=row.path.split("/");
          let ids=[];
          for (let i=1;i<arrays.length - 1;i++){
            ids.push(parseInt(arrays[i]))
          }
          this.activityForm.parent=ids;
        }
        this.getEmployeeList();
        this.getDepartTree();
			},
			//显示新增界面
			handleAdd: function () {
				this.activityFormVisible = true;
				this.activityForm = {
            id:null,
            name: "",
            intor: "",
            manage:{
              id:null
            },
            parent:{
              id:null
            },
            path:"",
            state:null
				}
				this.getEmployeeList();
        this.getDepartTree();
			},
			//编辑

			//新增
			save: function () {
				this.$refs.activityForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.activityFormLoading = true;
							let para = Object.assign({}, this.activityForm);
							if (para.parent!=null){
							  let array=para.parent;
							  para.parent={id:array[array.length-1]};
              }
							this.$http.put("/activity",para).then(result=>{
							  result=result.data;

							  if (result.success){
							    this.$message({message:'保存成功',type:'success'});
							    this.activityFormLoading=false;
							    this.query.currentPage=1;
							    this.getActivitys();
							    this.activityFormVisible=false;
                }else {

                  this.$message({message:'保存失败'+result.message,type:'ERROR'});
                }
              }).catch(result=>{
                this.$message({message:'保存失败',type:'ERROR'});
              })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
				  this.listLoading = true;
          ids:[];
          this.$http.patch("/activity",ids)
              .then(result=>{
                result=result.data;
                if (result.success){
                  this.$message({message: '删除成功', type: 'success'});
                  this.query.currentPage=1;
                  this.getActivitys();
                  this.listLoading = false;
                }else {
                  this.$message({message:"删除失败"+result.message, type: "ERROR"})
                }
              }).catch(()=>{
            this.$message({message:"删除失败", type: "ERROR"})
          })
        })
        /*this.listLoading = true;
        //NProgress.start();

        batchRemoveUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getActivitys();
        });
      }).catch(() => {

  });*/
			}
		},
		mounted() {
			this.getActivitys();
		}
	}

</script>

<style scoped>

</style>